package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.FamilyRelationshipBiz;
import com.njym.java.security.app.biz.FamilyTypeBiz;
import com.njym.java.security.app.entity.FamilyRelationship;
import com.njym.java.security.app.entity.FamilyType;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
@RequestMapping("familyType")
public class FamilyTypeController extends BaseController<FamilyTypeBiz,FamilyType> {

    @Autowired
    private  FamilyTypeBiz familyTypeBiz;

    @Autowired
    private FamilyRelationshipBiz familyRelationshipBiz;



    @RequestMapping(value = "/getFamilyName", method = RequestMethod.GET)
    @ResponseBody
    public Object getFamilyName(String userId){
        if (!userId.equals("undefined")) {
            Example example = new Example(FamilyType.class);
            example.createCriteria().andEqualTo("userId",Integer.parseInt(userId));
            List<FamilyType> list = baseBiz.selectByExample(example);
            return   ResponseUtil.ok(list);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    @ResponseBody
    public Object create(String userId,String familyName,String url){
        if (!userId.equals("undefined")) {
            FamilyType familyType = new FamilyType();
            familyType.setUserId(Integer.parseInt(userId));
            familyType.setFamilyName(familyName);
            familyType.setUrl(url);
            familyType.setDeleted(0);
            familyTypeBiz.insertFamily(familyType);
            FamilyRelationship  familyRelationship  = new  FamilyRelationship();
            familyRelationship.setUserId(Integer.parseInt(userId));
            familyRelationship.setFamilyTypeId(familyType.getId());
            familyRelationship.setDeleted(0);
            familyRelationship.setRelationName("本人");
            familyRelationshipBiz.insert(familyRelationship);
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }
    }


}